package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;

import metier.Entrainement;

public class DaoEntrainement 
{
	//methode static getLesEntrainements
	public static Vector <Entrainement> getLesEntrainements()
	{
		String nomDriver = "org.gjt.mm.mysql.Driver";
		
		final String url ="jdbc:mysql://localhost/bdd_centre";
		final String user="root";
		final String password="";
		
		Statement stLienBd;
		Connection connect;
		
		Vector <Entrainement> lesEntrainements;
		lesEntrainements = new Vector<Entrainement>();
		
		connect = null;
	
		try
		{
			// 1 - Chargement du driver

			Class.forName(nomDriver);
			
			// 2 - Etablissement de la connection
			connect = DriverManager.getConnection(url, user, password);
			
			//------------------------
			stLienBd = connect.createStatement();
		
	
			String req = "select * from Entrainement";
			ResultSet resultat = stLienBd.executeQuery(req);
		
			while(resultat.next())
			{
				Entrainement unEntrainement = new Entrainement (resultat.getInt("codeEntrainement"), resultat.getString("Date"),
											resultat.getString("HeureDebut"), resultat.getString("HeureFin"), resultat.getInt("noAdherent"));
				lesEntrainements.add(unEntrainement);
			}
			resultat.close();
		connect.close();
		//---------------------
		}
		catch(ClassNotFoundException e)
		{
			System.out.println("problème chargement de driver " + e.getException());
		}
		
		catch(SQLException e)
		{
			System.out.println("problème lors de la connexion à la bdd " + e.getMessage());
		}
		
		return lesEntrainements;
	}

	//ajoute un entrainement pour un adherent selectionner
	public static int ajouterEntrainement(Entrainement unEntrainement) throws SQLException
	{
			Statement stLienBd;
			Connection connect;
			connect = null;
			int nb;
			connect = MySqlConnection.getConnection("bdd_centre", "root", "");
			stLienBd = connect.createStatement();
			
			String req;
			req = "Insert into entrainement (date, heureDebut, heureFin, noAdherent) " +
					"values('" + unEntrainement.getDate() + "' , '" + unEntrainement.getHeureDeb() + "' ," +
							" '" +unEntrainement.getHeureFin() + "', '" + unEntrainement.getNumAdherent() + "')";
			//System.out.println(req);
			
			nb = stLienBd.executeUpdate(req);
			
			return nb;
    }
}
